# -*- coding: utf-8 -*-

from sklearn.linear_model.logistic import LogisticRegression
from sklearn.metrics import confusion_matrix,classification_report

def open_file(filename, mode = 'r'):
    return open(filename, mode, encoding='utf-8', errors='ignore')

def read_file(filename):
    data = []
    label = []
    with open_file(filename) as f:
        for line in f.readlines():
            current_line = line.strip().split('\t')
            line_array = []
            for i in current_line:
                line_array.append(float(i))
            data.append(line_array[:-1])
            label.append(line_array[-1])
    return data, label

train_path = 'data\\horseColicTraining.txt'
test_path = 'data\\horseColicTest.txt'

train_data, train_label = read_file(train_path)
test_data, test_label = read_file(test_path)

classifier = LogisticRegression()
classifier.fit(train_data, train_label)
predictions = classifier.predict(test_data)

print(confusion_matrix(test_label, predictions))

target_names = ['0', '1'] 
print('Accuracy: %s' % classification_report(test_label,predictions,target_names=target_names))











